import pymysql

conn = pymysql.connect(host="localhost",port=3306,
                       user="root",password="231231",
                       database="12345",charset="utf8mb4",
                       autocommit=True)

def con_sql(sql_code, params=None):
    try:
        conn.ping(reconnect=True)#是否连接正常
        # print(sql_code)
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        if params:
            cursor.execute(sql_code, params)
        else:
            cursor.execute(sql_code)

        conn.commit()

        conn.close()

        return cursor
    
    except pymysql.MySQLError as err_message:
        conn.rollback()

        conn.close()

        return type(err_message),err_message
    
#text
#customerName, PID, telephone, address, password

# customername = "王六"
# pid = "110101199001011238"
# phone = "13800138004"
# address = "大连市中山区"
# pwd = '345678'
# code = "INSERT INTO userInfo (customerName, PID, telephone, address, password) VALUES ('%s', '%s', '%s', '%s', '%s')" % (customername, pid, phone, address, pwd)
# cursor_ans = con_sql(code)

# print(cursor_ans.fetchall())
